/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tr.com.argela.tap.mot.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import tr.com.argela.tap.mot.dao.AuthenticationDao;
import tr.com.argela.tap.mot.dao.DbManager;
import tr.com.argela.tap.mot.dao.TestCaseDao;
import tr.com.argela.tap.mot.dao.TestPointDao;
import tr.com.argela.tap.mot.logger.LoggerFactory;
import tr.com.argela.tap.mot.models.TestCase;
import tr.com.argela.tap.mot.models.TestPoint;
import tr.com.argela.tap.mot.models.User;

/**
 * @author arge
 */
public class TestCaseServlet extends HttpServlet {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger("servlets");

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("case").equals("createtestcase")) {

            createtestcase(request, response);
        } else if (request.getParameter("case").equals("updatecase")) {
            updatetestcase(request, response);

        } else if (request.getParameter("case").equals("executecase")) {
            executecase(request, response);

        } else if (request.getParameter("case").equals("createtestcase2")) {

            createtestcase2(request, response);
        } else if (request.getParameter("case").equals("executecase2")) {
            executecase2(request, response);
        } else if (request.getParameter("case").equals("updatecase2")) {
            updatetestcase2(request, response);

        }
    }

    public void createtestcase(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            String name = request.getParameter("tcname");
            String bussinesreq = request.getParameter("brq");
            String testPointId = request.getParameter("testpointname");

            String precondition = request.getParameter("precon");
            String definition = request.getParameter("definition");
            String expectedres = request.getParameter("expected");
            String checkitem = request.getParameter("check");
            String notes = request.getParameter("notes");
            String art = request.getParameter("art");
            String userId = request.getParameter("username");

            if (name != null && bussinesreq != null && testPointId != null && precondition != null && definition != null
                    && expectedres != null && checkitem != null && notes != null && art != null && userId != null) {
                TestCaseDao autDao = new TestCaseDao();
                System.out.print("test case list details" + name + bussinesreq + testPointId + precondition + definition + expectedres
                        + checkitem + notes + art + userId);
                User user = new User();
                user.setId(Long.parseLong(userId));
                TestPoint testpoint = new TestPoint();
                testpoint.setId(Long.parseLong(testPointId));
                int result = autDao.addTestCase(dbManager, name, bussinesreq, testpoint, precondition, definition, expectedres, checkitem,
                        notes, art, user);
                TestCase testcase = null;
                if (result > 0) {
                    testcase = new TestCase();
                    testcase.setName(name);
                    testcase.setBreq(bussinesreq);
                    TestPointDao pointdao = new TestPointDao();
                    testpoint = pointdao.getTestPointById(testpoint.getId());
                    testcase.setTestPoint(testpoint);
                    testcase.setPrecondition(precondition);
                    testcase.setDescription(definition);
                    testcase.setExpectedRes(expectedres);
                    testcase.setCheck(checkitem);
                    testcase.setNotes(notes);
                    testcase.setArtno(art);
                    AuthenticationDao autDao2 = new AuthenticationDao();
                    user = autDao2.getUserById(user.getId());
                    testcase.setUser(user);
                }
                request.getSession().setAttribute("case", testcase);
            }
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("caseproperties.jsp");

    }

    public void createtestcase2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            String name = request.getParameter("tcname");
            String bussinesreq = request.getParameter("brq");
            String testPointId = request.getParameter("testpointname");

            String precondition = request.getParameter("precon");
            String definition = request.getParameter("definition");
            String expectedres = request.getParameter("expected");
            String checkitem = request.getParameter("check");
            String notes = request.getParameter("notes");
            String art = request.getParameter("art");
            String userId = request.getParameter("username");

            if (name != null && bussinesreq != null && testPointId != null && precondition != null && definition != null
                    && expectedres != null && checkitem != null && notes != null && art != null && userId != null) {
                TestCaseDao autDao = new TestCaseDao();
                System.out.print("test case list details" + name + bussinesreq + testPointId + precondition + definition + expectedres
                        + checkitem + notes + art + userId);
                User user = new User();
                user.setId(Long.parseLong(userId));
                TestPoint testpoint = new TestPoint();
                testpoint.setId(Long.parseLong(testPointId));
                int result = autDao.addTestCase(dbManager, name, bussinesreq, testpoint, precondition, definition, expectedres, checkitem,
                        notes, art, user);
                TestCase testcase = null;
                if (result > 0) {
                    testcase = new TestCase();
                    testcase.setName(name);
                    testcase.setBreq(bussinesreq);
                    TestPointDao pointdao = new TestPointDao();
                    testpoint = pointdao.getTestPointById(testpoint.getId());
                    testcase.setTestPoint(testpoint);
                    testcase.setPrecondition(precondition);
                    testcase.setDescription(definition);
                    testcase.setExpectedRes(expectedres);
                    testcase.setCheck(checkitem);
                    testcase.setNotes(notes);
                    testcase.setArtno(art);
                    AuthenticationDao autDao2 = new AuthenticationDao();
                    user = autDao2.getUserById(user.getId());
                    testcase.setUser(user);
                }
                request.getSession().setAttribute("case", testcase);
            }
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("caseproperties2.jsp");

    }

    public void updatetestcase(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            TestCaseDao caseDao = new TestCaseDao();
            Long caseId = Long.parseLong(request.getParameter("caseId"));
            String name = request.getParameter("tcasename");
            String bussinesreq = request.getParameter("breq");
            String testPointId = request.getParameter("pname");

            String precondition = request.getParameter("precon");
            String definition = request.getParameter("casedescription");
            String expectedres = request.getParameter("expected");
            String checkitem = request.getParameter("check");

            String userId = request.getParameter("username");

            System.out.print("test case list details" + name + bussinesreq + testPointId + precondition + definition + expectedres
                    + checkitem + userId + caseId);
            User user = new User();
            user.setId(Long.parseLong(userId));
            TestPoint testpoint = new TestPoint();
            testpoint.setId(Long.parseLong(testPointId));

            int result = caseDao.UpdateTestCase(dbManager, name, bussinesreq, testpoint, precondition, definition, expectedres, checkitem,
                    user, caseId);
            TestCase testcase = null;
            if (result > 0) {
                testcase = new TestCase();
                testcase.setName(name);
                testcase.setBreq(bussinesreq);
                TestPointDao pointdao = new TestPointDao();
                testpoint = pointdao.getTestPointById(testpoint.getId());
                testcase.setTestPoint(testpoint);
                testcase.setPrecondition(precondition);
                testcase.setDescription(definition);
                testcase.setExpectedRes(expectedres);
                testcase.setCheck(checkitem);

                AuthenticationDao autDao2 = new AuthenticationDao();
                user = autDao2.getUserById(user.getId());
                testcase.setUser(user);
            }
            request.getSession().setAttribute("case", testcase);
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }

        response.sendRedirect("caseproperties.jsp");
    }

    public void updatetestcase2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            TestCaseDao caseDao = new TestCaseDao();
            Long caseId = Long.parseLong(request.getParameter("caseId"));
            String name = request.getParameter("tcasename");
            String bussinesreq = request.getParameter("breq");
            String testPointId = request.getParameter("pname");

            String precondition = request.getParameter("precon");
            String definition = request.getParameter("casedescription");
            String expectedres = request.getParameter("expected");
            String checkitem = request.getParameter("check");

            String userId = request.getParameter("username");

            System.out.print("test case list details" + name + bussinesreq + testPointId + precondition + definition + expectedres
                    + checkitem + userId + caseId);
            User user = new User();
            user.setId(Long.parseLong(userId));
            TestPoint testpoint = new TestPoint();
            testpoint.setId(Long.parseLong(testPointId));

            int result = caseDao.UpdateTestCase(dbManager, name, bussinesreq, testpoint, precondition, definition, expectedres, checkitem,
                    user, caseId);
            TestCase testcase = null;
            if (result > 0) {
                testcase = new TestCase();
                testcase.setName(name);
                testcase.setBreq(bussinesreq);
                TestPointDao pointdao = new TestPointDao();
                testpoint = pointdao.getTestPointById(testpoint.getId());
                testcase.setTestPoint(testpoint);
                testcase.setPrecondition(precondition);
                testcase.setDescription(definition);
                testcase.setExpectedRes(expectedres);
                testcase.setCheck(checkitem);

                AuthenticationDao autDao2 = new AuthenticationDao();
                user = autDao2.getUserById(user.getId());
                testcase.setUser(user);
            }
            request.getSession().setAttribute("case", testcase);
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }

        response.sendRedirect("caseproperties2.jsp");
    }

    public void executecase(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            TestCaseDao caseDao = new TestCaseDao();
            Long caseId = Long.parseLong(request.getParameter("caseId"));
            String result = request.getParameter("result");
            String notes = request.getParameter("notes");
            String artno = request.getParameter("artno");
            String name = request.getParameter("tcasename");
            System.out.print("test case list details" + result + notes + artno + name + caseId);

            int results = caseDao.executeTestCase(dbManager, result, notes, artno, name, caseId);
            TestCase testcase = null;
            if (results > 0) {
                testcase = new TestCase();

                testcase.setResult(result);
                testcase.setNotes(notes);
                testcase.setArtno(artno);
                testcase.setName(name);
            }
            request.getSession().setAttribute("case", testcase);
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("executecaseproperties.jsp");

    }

    public void executecase2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {

            TestCaseDao caseDao = new TestCaseDao();
            Long caseId = Long.parseLong(request.getParameter("caseId"));
            String result = request.getParameter("result");
            String notes = request.getParameter("notes");
            String artno = request.getParameter("artno");
            String name = request.getParameter("tcasename");
            System.out.print("test case list details" + result + notes + artno + name + caseId);

            int results = caseDao.executeTestCase(dbManager, result, notes, artno, name, caseId);
            TestCase testcase = null;
            if (results > 0) {
                testcase = new TestCase();

                testcase.setResult(result);
                testcase.setNotes(notes);
                testcase.setArtno(artno);
                testcase.setName(name);
            }
            request.getSession().setAttribute("case", testcase);
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("executecaseproperties2.jsp");

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * 
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
